//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
// 			VisualNovelToolkit		     /_/_/_/_/_/_/_/_/_/.
// Copyright ©2013 - Sol-tribe.	/_/_/_/_/_/_/_/_/_/.
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// Game object cache. To Activate a Deactivated GameObject .
/// </summary>
public class GOCache : MonoBehaviour {
	static GOCache m_Instance = null;	
	static private Dictionary<string,GameObject> m_ObjMap = new Dictionary<string,GameObject>();
	
	/// <summary>
	/// When level is loaded, Clear Object Map.
	/// </summary>
	/// <param name='levelIndex'>
	/// Level index.
	/// </param>
	void OnLevelWasLoaded( int levelIndex ){
		ViNoDebugger.Log( "GO" , "OnLevelWasLoaded and Object Cache was Cleaned up." );
//		m_ObjMap = new Dictionary<string, GameObject>();			
	}
	
	/// <summary>
	/// Add the specified obj and active.
	/// </summary>
	/// <param name='obj'>
	/// Object.
	/// </param>
	/// <param name='active'>
	/// Active.
	/// </param>
	static public void Add ( GameObject obj  ){		
		Add( obj.name , obj );
	}

	static public void Add( string key , GameObject obj ){
		ViNoDebugger.Log( "GO" , "GOCache Add:" + key );
		if (m_Instance == null)
		{				
			GameObject go = new GameObject("_ViNo_ObjectCache");
			go.hideFlags = HideFlags.HideInHierarchy;
			m_Instance = go.AddComponent<GOCache>();			
			
			m_ObjMap = new Dictionary<string, GameObject>();			
		}
		m_ObjMap[ key ] = obj;
	}
	
	/// <summary>
	/// SetActive name Object.
	/// </summary>
	/// <returns>
	/// return the name GameObject, if not found , return null.
	/// </returns>
	static public GameObject SetActive( string name , bool active ){		
		ViNoDebugger.Log( "GO" , "GOCache SetActive:" + name );
		if( m_ObjMap.ContainsKey( name) ) 
		{			
			if( m_ObjMap[ name ] != null ){
				m_ObjMap[ name ].SetActive( active );
			}
			return m_ObjMap[ name ];
		}
		else{
			ViNoDebugger.Log( "GO" , "GOCache.SetActive:" + name + " not Contains." ) ;	
			return null;
		}
	}

	static public GameObject Clean( string name ){		
		ViNoDebugger.Log( "GO" , "GOCache Clean:" + name );
		if( m_ObjMap.ContainsKey( name) ) 
		{
			GameObject obj = m_ObjMap[ name ];
			m_ObjMap[ name ] = null;
			return obj;
		}
		else{
			ViNoDebugger.Log( "GO" , "GameObjectCache.Clean:" + name + " not Contains." ) ;	
			return null;
		}
	}	
	
	static public Dictionary<string,GameObject>.KeyCollection GetKeys(){
		return m_ObjMap.Keys;
	}

	static public GameObject Get( string key ){
		return m_ObjMap[ key ];
	}
}
